<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * listitemrequest.php
 * Began: November 2007
 * 
 * 
 ******************************/
 
define('EQDKP_INC', true);
define('IN_ADMIN', true);
$eqdkp_root_path = './../';
include_once($eqdkp_root_path . 'common.php');

$user->check_auth('a_item_');

$sort_order = array(
    0 => array('request_fulfilled', 'request_fulfilled desc'),
    1 => array('item_requester', 'item_requester desc'),
    2 => array('item_name', 'item_name desc'),
    3 => array('id.item_name', 'id.item_name desc')
);

$current_order = switch_order($sort_order);

$total_items = $db->query_first('SELECT count(*) FROM ' . ITEM_REQUEST_TABLE);
$start = ( isset($_GET['start']) ) ? $_GET['start'] : 0;

$sql = 'SELECT ir.item_id, 
               ir.item_name, 
               ir.item_requester, 
               ir.request_fulfilled,
               id.dropped_from as dropped_from,
               id.item_data_id as data_id
	FROM ' . ITEM_REQUEST_TABLE . ' ir
        LEFT JOIN ' . ITEM_DATA_TABLE . ' id on ir.item_name = id.item_name
	ORDER BY '.$current_order['sql']. '
	LIMIT '.$start.','.$user->data['user_ilimit'];

$listitems_footcount = sprintf($user->lang['listpurchased_footcount'], $total_items, $user->data['user_ilimit']);
$pagination = generate_pagination('listitemrequest.php'.$SID.'&amp;o='.$current_order['uri']['current'], $total_items, $user->data['user_ilimit'], $start);

if ( !($items_result = $db->query($sql)) )
{
    message_die('Could not obtain item information', 'Database error', __FILE__, __LINE__, $sql);
}

while ( $item = $db->fetch_record($items_result) )
{
    $tpl->assign_block_vars('items_row', array(
        'ROW_CLASS' => $eqdkp->switch_row_class(),
        'DROPPED_FROM' => (!empty($item['dropped_from']) ? $item['dropped_from'] : ''),
        'DATA_ID' => (!empty($item['data_id']) ? $item['data_id'] : '-1'),
        'REQUESTER' => ( !empty($item['item_requester']) ) ? $item['item_requester'] : '&lt;<i>Not Found</i>&gt;',
        'U_VIEW_REQUESTER' => ( !empty($item['item_requester']) ) ? '../viewmember.php'.$SID.'&amp;' . URI_NAME . '='.$item['item_requester'] : '',
        'NAME' => stripslashes($item['item_name']),
	'REQUEST_FULFILLED' => ($item['request_fulfilled'] == 1) ? 'Yes' : 'No',
        'U_VIEW_ITEM' => 'additemrequest.php'.$SID.'&amp;' . URI_ITEM . '='.$item['item_id'])
    );
}
$db->free_result($items_result);

$tpl->assign_vars(array(
    'L_REQUEST_FULFILLED' => $user->lang['req_fulfilled'],
    'L_REQUESTER' => $user->lang['requester'],
    'L_ITEM' => $user->lang['item'],
    'L_DROPPED_FROM' => $user->lang['dropped_from'],
    
    'O_REQUEST_FULFILLED' => $current_order['uri'][0],
    'O_REQUESTER' => $current_order['uri'][1],
    'O_NAME' => $current_order['uri'][2],
    'O_DROPPED_FROM' => $current_order['uri'][3],
    
    'U_LIST_ITEMS' => 'listitemrequest.php'.$SID.'&amp;',
    
    'START' => $start,
    'S_HISTORY' => true,
    'LISTITEMS_FOOTCOUNT' => $listitems_footcount,
    'ITEM_PAGINATION' => $pagination)
);

$eqdkp->set_vars(array(
    'page_title'    => sprintf($user->lang['admin_title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['listpurchased_title'],
    'template_file' => 'listitemrequest.html',
    'display'       => true)
);
?>
